package csu.web.jpetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import csu.web.jpetstore.domain.Category;
import csu.web.jpetstore.domain.Product;
import csu.web.jpetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class productPreview extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String attr=req.getParameter("categoryId");
        CatalogService service=new CatalogService();
        Category category= service.getCategory(attr);
        List<Product> temp=service.getProductListByCategory(attr);
        List<String> result=new ArrayList<>();
        for(int i=0;i< temp.size();i++){
            result.add(temp.get(i).getName());
        }

        String r = JSON.toJSONString(result);
        System.out.println(r);

        resp.setContentType("text/json");
        PrintWriter out=resp.getWriter();
        out.println(r);
    }
}
